package cn.holy.app.xcddt.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.holy.app.xcddt.R
import cn.holy.app.xcddt.app.App
import cn.holy.app.xcddt.domain.RecordGroup
import cn.holy.app.xcddt.net.callback.LoadingCallback
import cn.holy.app.xcddt.repository.RecordRepository
import cn.holy.app.xcddt.utils.HDateUtils
import com.alibaba.android.arouter.launcher.ARouter
import com.orhanobut.logger.Logger
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder
import kotlinx.android.synthetic.main.fragment_list.*
import javax.inject.Inject

/**
 * Created by 69568 on 2018/2/24.
 */

class ArchiveAllFragment : Fragment() {


    @Inject
    lateinit var recordRepository: RecordRepository
    private var mRecordBeans: ArrayList<RecordGroup> = ArrayList()
    private var commonAdapter: CommonAdapter<RecordGroup>? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_list, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        App.appComponent.inject(this)
        status = arguments!!.getInt("status")
        initData()
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        Logger.d(" setUserVisibleHint() --> isVisibleToUser = " + isVisibleToUser)

        if (isVisibleToUser) {
            refreshLayout?.autoRefresh()
        }
        super.setUserVisibleHint(isVisibleToUser)
    }

    private fun initData() {
        val layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        refreshLayout.setOnRefreshListener {
            page = 1
            loadArchives(page)
        }
        refreshLayout.setOnLoadMoreListener {
            page++
            loadArchives(page)
        }
        listview.layoutManager = layoutManager
        listview.addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))
        commonAdapter = object : CommonAdapter<RecordGroup>(context, R.layout.layout_record_group_item, mRecordBeans) {

            override fun convert(holder: ViewHolder, recordGroup: RecordGroup, position: Int) {
                try {
                    holder.getView<TextView>(R.id.tv_group).text = "第" + recordGroup.record_num + "次巡查"
                    holder.getView<TextView>(R.id.tv_group_time).text = HDateUtils.dateToStr(recordGroup.record_date!!)
                    holder.getView<TextView>(R.id.tv_record_username).text = recordGroup.record_username
                    holder.getView<TextView>(R.id.tv_record_people).text = recordGroup.record_people
                    val view = holder.getView<TextView>(R.id.upload_status)
                    view.visibility = View.GONE
                    holder.getView<TextView>(R.id.btn_end_group).visibility = View.GONE
//                    view.visibility = View.GONE
                }catch (e:Exception){

                }

//                holder.getView<TextView>(R.id.tv_record_no).text = "案卷编号：" + recordBean.record_no
//                holder.getView<TextView>(R.id.tv_message).text = recordBean.message
//                holder.getView<TextView>(R.id.tv_address).text = recordBean.address
//                holder.getView<TextView>(R.id.tv_status).text = recordBean.status_label
//                holder.getView<TextView>(R.id.tv_record_people).text = recordBean.record_people
//                holder.getView<TextView>(R.id.tv_record_group).text = "第" + (recordBean.record_number ) + "次巡查"
//
//                holder.getView<TextView>(R.id.tv_time).text = "记录时间：" + HDateUtils.dateToStrLong(recordBean.record_date)
            }
        }
        commonAdapter?.setOnItemClickListener(object : MultiItemTypeAdapter.OnItemClickListener {
            override fun onItemClick(view: View?, holder: RecyclerView.ViewHolder?, position: Int) {
                val item = mRecordBeans[position]
                ARouter.getInstance().build("/app/archives/group_list").withSerializable("group", item)
                        .navigation()
            }

            override fun onItemLongClick(view: View?, holder: RecyclerView.ViewHolder?, position: Int): Boolean {
                return true
            }

        })
        listview.adapter = commonAdapter
        if (userVisibleHint) {
            refreshLayout.autoRefresh()
        }
    }

    private var page = 1
    private var status = 0
    private fun loadArchives(page: Int) {
        recordRepository.loadArchivesByGroup(page,object : LoadingCallback<ArrayList<RecordGroup>> {
            override fun onStart() {
            }

            override fun onError(msg: String) {
            }

            override fun onSuccess(t: ArrayList<RecordGroup>) {
                if (page == 1) {
                    mRecordBeans.clear()
                    refreshLayout.finishRefresh()
                } else {
                    refreshLayout.finishLoadMore()
                }
                mRecordBeans.addAll(t)
                commonAdapter?.notifyDataSetChanged()
            }

            override fun onSuccess() {
            }

            override fun onCompleted() {
            }

        })
    }
}